package com.yej.aiPolice.ui.fragment

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yej.aiPolice.R
import com.yej.aiPolice.dagger2.component.DaggerHomeFragmentComponent
import com.yej.aiPolice.dagger2.module.HomeFragmentModule
import com.yej.aiPolice.model.beans.Denews
import com.yej.aiPolice.presenter.HomeFragmentPresenter
import com.yej.aiPolice.ui.adpter.HomeRVAdapter
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.find
import org.jetbrains.anko.toast
import javax.inject.Inject


class HomeFragment : Fragment() {

    lateinit var homeRVAdapter:HomeRVAdapter
    lateinit var rvHome:RecyclerView
    @Inject
    lateinit var homeFragmentPresenter:HomeFragmentPresenter
    //创造view才能看到Fragment效果
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = View.inflate(activity, R.layout.fragment_home, null)
        rvHome = view.find<RecyclerView>(R.id.rv_home)
        rvHome.layoutManager = LinearLayoutManager(activity)  //从上到下的列表视图
        homeRVAdapter = HomeRVAdapter(activity)
        rvHome.adapter =homeRVAdapter
//        homeFragmentPresenter = HomeFragmentPres  enter(this)

        //TODO 解耦P层与V层，通过dagger2依赖注入生成HomeFragmentPresenter
        DaggerHomeFragmentComponent.builder().homeFragmentModule(HomeFragmentModule(this)).build().inject(this)
        distance = 120.dp2px()
        return view
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素),
     * 把转换功能添加的int类中做为扩展函数
     */
    fun Int.dp2px(): Int {
        val scale = resources.displayMetrics.density
        return (toFloat() * scale + 0.5f).toInt()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    val datas:ArrayList<String> = ArrayList()
    var sum:Int = 0
    var distance:Int = 0
    var alpha:Int = 55
    private fun initData(){
//        for(i in 0 until 100){
//            datas.add("我是条目：$i")
//        }
        homeFragmentPresenter.getHomeInfo()
//        homeRVAdapter.setData(datas)


    }

    fun onHomeSuccess(newsList: List<Denews>) {
        //展示成果的页面
//        context?.toast("成功加载首页数据")
        homeRVAdapter.setData(newsList)
        // 有数据可以滚动才可以监听滚动事件
        rvHome.setOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                sum += dy
                if(sum > distance){
                    alpha = 255
                }else{
                    alpha = sum*200/distance //计算一个移动比例，产生渐变效果
                    alpha += 55
                }
                ll_title_container.setBackgroundColor(Color.argb(alpha,0x31,0x90,0xe8))

                Log.e("home","sum :$sum")
            }

        })



    }

    fun onHomeFailed() {
        context?.toast("加载首页数据失败")
    }
}